<template>
    <div class="common">
        <el-card class="doctor-card">
            <template #header>
                <h3 v-text="doctor.name + '（' + doctor.title + '）'"></h3>
            </template>
            <div class="doctor">
                <div class="doctor-img-box">
                    <el-image v-if="doctor.user_img" :src="doctor.user_img" class="doctor-img"></el-image>
                    <el-image v-else src="src\assets\imgs\方形默认头像.png" class="doctor-img"></el-image>
                    <el-rate v-if="doctor.score" v-model="doctor.score" :colors="colors" disabled show-score
                        text-color="#ff9900" :score-template="`${doctor.score?.toFixed(1) || '0.0'} 分`" size="large" />
                </div>
                <p class="doctor-desc" v-text="doctor.description"></p>
            </div>
            <!-- 患者评价列表 -->
            <el-card class="evaluate-card">
                <div class="evaluate-list">
                    <div v-if="evaluations.length == 0">
                        <p class="no-evaluate">暂无评价</p>
                    </div>
                    <div class="evaluate-item" v-else v-for="item in evaluations" :key="item.id">
                        <div class="resident-info">
                            <el-image v-if="item.user_img" :src="item.user_img" class="resident-img"></el-image>
                            <el-image v-else src="src\assets\imgs\方形默认头像.png" class="resident-img"></el-image>
                            <span v-text="item.resident"></span>
                        </div>
                        <div class="evaluate-content">
                            <el-rate v-model="item.score" :colors="colors" disabled show-score text-color="#ff9900"
                                :score-template="`${item.score?.toFixed(1) || '0.0'}`" />
                            <p v-text="item.content"></p>
                            <p class="time" v-text="item.time.substring(0, 10) + ' ' + item.time.substring(11, 16)"></p>
                        </div>
                    </div>
                </div>
                <!-- 分页区域 -->
                <el-pagination v-if="params.total > 0" :current-page="params.currentPage" :page-size="params.pageSize"
                    background layout="total, prev, pager, next" :total="params.total"
                    @current-change="handleCurrentChange" />
            </el-card>
        </el-card>
    </div>
</template>

<script>
import { doctorInfoFindAPI } from '@/api/doctor';
import { evaluationComputeAPI, evaluationsFindAPI } from '@/api/evaluate';
import { ElMessage } from 'element-plus';

export default {
    data() {
        return {
            params: {
                // 查询参数
                doctorId: this.$route.params.doctorId,
                doctorIds: [parseInt(this.$route.params.doctorId)],
                // 分页参数
                total: 0,
                currentPage: 1,
                pageSize: 7,
            },
            evaluations: [],
            doctor: {}
        }
    },
    methods: {
        loadEvaluate() {
            // 使用Promise.all来确保所有API请求完成后再更新视图（解决医生评分可能显示为0.0的问题）
            Promise.all([
                doctorInfoFindAPI(this.params),
                evaluationsFindAPI(this.params),
                evaluationComputeAPI(this.params)
            ]).then(results => {
                const [doctorResp, evaluationsResp, scoreResp] = results;

                if (doctorResp.data.code == 2000) {
                    this.doctor = doctorResp.data.data;
                    console.log(this.doctor);
                } else {
                    ElMessage.error(doctorResp.data.message);
                }

                if (evaluationsResp.data.code == 2000) {
                    this.evaluations = evaluationsResp.data.datas;
                    this.params.total = evaluationsResp.data.total;
                } else {
                    ElMessage.error(evaluationsResp.data.message);
                }

                if (scoreResp.data.code == 2000) {
                    this.doctor.score = scoreResp.data.datas[0].score;
                } else {
                    ElMessage.error(scoreResp.data.message);
                }
            }).catch(error => {
                ElMessage.error('请求失败，请稍后再试');
            });
        }
    },
    created() {
        this.loadEvaluate();
    }
}
</script>

<style scoped>
.el-pagination {
    margin-top: 20px;
    justify-content: center;
    align-items: center;
}

.common {
    width: 100vw;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.doctor-card {
    width: 100%;
}

.doctor {
    display: flex;
    /* 流式布局子元素间距 */
    gap: 20px;
    width: 100%;
}

.doctor .doctor-img {
    border-radius: 6px;
    width: 200px;
}

.doctor .doctor-title {
    font-size: 14px;
    font-family: '楷体', '仿宋';
    color: #909399;
}

.doctor .doctor-img-box {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.doctor .doctor-desc {
    font-size: 14px;
    color: #606266;
    line-height: 1.5;
    margin-top: 10px;
    letter-spacing: 0.5px;
    /* 首行缩进 */
    text-indent: 2em;
}

.evaluate-card {
    margin-top: 20px;
    height: 100%;
    overflow: auto;
    padding: 20px;
    width: 100%;
}

.evaluate-card .evaluate-list {
    display: flex;
    flex-direction: column;
    gap: 20px;
    overflow: auto;
    width: 100%;
}

.evaluate-card .evaluate-list .no-evaluate {
    width: 100%;
    text-align: center;
}

.evaluate-card .evaluate-list .evaluate-item {
    display: flex;
}

.evaluate-card .evaluate-list .evaluate-item .resident-info {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
}

.evaluate-card .evaluate-list .evaluate-item .resident-info .resident-img {
    width: 50px;
    height: 50px;
    border-radius: 50%;
}

.evaluate-card .evaluate-list .evaluate-item .evaluate-content {
    width: 100%;
    margin-left: 10px;
    padding: 10px;
    border-radius: 10px;
    background-color: rgba(187, 221, 238, 0.55);
    overflow: auto;
    text-align: left;
    white-space: pre-wrap;
    word-wrap: break-word;
    word-break: break-all;
    word-break: break-word;
    overflow-wrap: break-word;
    line-height: 1.5;
    font-size: 14px;
    color: #606266;
    font-family: '楷体', '仿宋';
    font-weight: 400;
    letter-spacing: 0.5px;
}

.evaluate-card .evaluate-list .evaluate-item .evaluate-content .time {
    font-size: 12px;
    color: #909399;
    font-family: '楷体', '仿宋';
    font-weight: 400;
    letter-spacing: 0.5px;
    margin-top: 10px;
    text-align: right;
    margin-right: 10px;
}

.evaluate-card {
    background-color: #eff;
}

@media (max-width: 768px) {
    .doctor {
        flex-direction: column;
        align-items: center;
        gap: 0;
    }

    .doctor .doctor-img {
        width: 50vw;
        height: auto;
    }

    .doctor .doctor-desc {
        margin-top: 0px;
    }

    .el-card {
        width: 100vw;
    }

    .evaluate-card {
        margin-top: 1vh;
        padding: 0px;
        width: 90vw;
        --el-card-padding: 10px;
    }

    .evaluate-card .evaluate-list {
        gap: 8px;
    }

    .evaluate-card .evaluate-list .evaluate-item .resident-info {
        font-size: 12px;
    }

    .evaluate-card .evaluate-list .evaluate-item .resident-info .resident-img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
    }
}
</style>